const express = require('express');
const router = express.Router();
const userService = require('@/services/userService');
const validateChecks = require('@/middlewares/validatorPayload');
const { check, param } = require('express-validator');



router.post('/register', validateChecks([
    check('user_name', '用户名不能为空').notEmpty(),
    check('password', '密码不能为空或长度不能低于5位').isLength({ min: 5 }),
    check('phone', '手机号不能为空或电话格式不对').isMobilePhone()
]), async function (req, res, next) {
    try {
        await userService.createUser(req.body);
        res.createResponse(200, '用户注册成功');
    } catch (err) {
        res.createResponse(500, err.message);
    }
});


router.post('/login', validateChecks([
    check('password', '密码不能为空或长度不能低于5位').isLength({ min: 5 }),
    check('phone', '手机号不能为空或电话格式不对').isMobilePhone()
]), async function (req, res, next) {
    try {
        const { phone , password  } = req.body;
        const data = await userService.veryLogin({phone,password});
        res.createResponse(200, '登录成功！', data)
    } catch (err) {
        res.createResponse(500, err.message);
    }
});


router.post('/loginOut', validateChecks([
    check('uid', 'uid不能为空').notEmpty(),
]), async function (req, res, next) {
    try {
        const { uid } = req.body;
        const data = await userService.loginOut(uid);
        res.createResponse(200, '退出成功！', data)
    } catch (err) {
        res.createResponse(500, err.message);
    }
});



router.post('/verify', validateChecks([
    check('code', '验证码不能为空').notEmpty(),
    check('email', '邮箱不能为空').notEmpty(),
]), async function (req, res, next) {
   
    try {
        await userService.verify(req.body);
        res.createResponse(200, '用户激活成功');
    } catch (err) {
        res.createResponse(500, err.message);
    }
});



module.exports = router;